<template>
  <div class="node-wrap">
    <!--    发起节点-->
    <div class="node-wrap-box start-node" @click="show">
      <div class="title" style="background: #576a95">
        <el-icon class="icon"><el-icon-user-filled /></el-icon>
        <span>{{ nodeConfig.nodeName }}</span>
      </div>
      <div class="content">
        <span>{{ toText(nodeConfig) }}</span>
      </div>
    </div>
    <add-node v-model="nodeConfig.childNode"></add-node>
    <el-drawer
      title="发起人"
      v-model="drawer"
      destroy-on-close
      append-to-body
      :size="500"
    >
      <template #header>
        <div class="node-wrap-drawer__title">
          <label @click="editTitle" v-if="!isEditTitle"
            >{{ form.nodeName
            }}<el-icon class="node-wrap-drawer__title-edit"><el-icon-edit /></el-icon
          ></label>
          <el-input
            v-if="isEditTitle"
            ref="nodeTitle"
            v-model="form.nodeName"
            clearable
            @blur="saveTitle"
            @keyup.enter="saveTitle"
          ></el-input>
        </div>
      </template>
      <el-container>
        <el-main style="padding: 0 20px 20px 20px">
          <el-form label-position="top">
            <el-form-item label="发起人人员类型">
              <el-select v-model="form.setType">
                <el-option v-for="(setType, index) in setTypeList"
                           :key="setType.dictKey" :value="setType.dictKey" :label="setType.dictValue"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item v-if="form.setType === 3" label="选择角色">
              <el-button
                type="primary"
                icon="el-icon-plus"
                round
                @click="selectHandle(3, form.nodeRoleList)"
                >选择角色</el-button
              >
              <div class="tags-list">
                <el-tag
                  v-for="(role, index) in form.nodeRoleList"
                  :key="role.id"
                  type="info"
                  closable
                  @close="delRole(index)"
                  >{{ role.name }}</el-tag
                >
              </div>
            </el-form-item>
            <el-alert
              v-if="form.nodeRoleList.length == 0"
              title="不指定则默认所有人都可发起此审批"
              type="info"
              :closable="false"
            />
            <el-divider></el-divider>
            <el-form-item label="pc配置还原页面">
              <el-input
                style="margin-bottom: 14px"
                v-model="form.formUrl"
                placeholder="请填写formUrl地址"
              />
              <el-radio-group v-model="form.formMode">
                <p style="width: 100%">
                  <el-radio :value="1">仅加载当前页面组件</el-radio>
                </p>
                <p style="width: 100%">
                  <el-radio :value="2">按节点顺序叠加页面组件</el-radio>
                </p>
              </el-radio-group>
            </el-form-item>
            <el-divider></el-divider>
            <el-form-item label="app配置还原页面">
              <el-input
                  style="margin-bottom: 14px"
                  v-model="form.appFormUrl"
                  placeholder="请填写appFormUrl地址"
              />
            </el-form-item>
          </el-form>
        </el-main>
        <el-footer>
          <el-button type="primary" @click="save">保存</el-button>
          <el-button @click="drawer = false">取消</el-button>
        </el-footer>
      </el-container>
    </el-drawer>

    <!--    自定义抽屉-->
    <userConfig v-model="this.form" :drawer="drawerForUserConfig" @update:drawerForUserConfig="handleDrawerForUserConfig"></userConfig>
  </div>
</template>

<script>
import addNode from "./addNode.vue";
import userConfig from '@/components/scWorkflow/custom/userConfig.vue';

export default {
  inject: ["select"],
  props: {
    modelValue: { type: Object, default: () => {} },
  },
  components: {
    addNode,
  },
  data() {
    return {
      nodeConfig: {},
      drawer: false,
      drawerForUserConfig: false,
      isEditTitle: false,
      form: {},
      setTypeList: [
        {dictKey: 0,dictValue: '所有人'},
        {dictKey: 3,dictValue: '指定角色'},
        {dictKey: 8,dictValue: '指定组织'},
        {dictKey: 15,dictValue: '用户配置发起人员'},
      ],
    };
  },
  watch: {
    modelValue() {
      this.nodeConfig = this.modelValue;
    },
  },
  mounted() {
    const params = {
      dictFields: 'setTypeStart',
    };
    $api.common.getBatchDict.post(params).then((res) => {
      if(res.result.setTypeStart && res.result.setTypeStart.length > 0){
        res.result.setTypeStart.forEach(ele=>{
          ele.dictKey = Number(ele.dictKey);
        })
      }
      this.setTypeList = res.result.setTypeStart;
    });
    this.nodeConfig = this.modelValue;
  },
  methods: {
    show() {
      this.form = {};
      this.form = JSON.parse(JSON.stringify(this.nodeConfig));
      if(this.form.customDesignForUserConfig){
        this.drawerForUserConfig = true;
        return;
      }
      this.isEditTitle = false;
      this.drawer = true;
    },
    editTitle() {
      this.isEditTitle = true;
      this.$nextTick(() => {
        this.$refs.nodeTitle.focus();
      });
    },
    saveTitle() {
      this.isEditTitle = false;
    },
    selectHandle(type, data) {
      this.select(type, data);
    },
    delRole(index) {
      this.form.nodeRoleList.splice(index, 1);
    },
    save() {
      this.$emit("update:modelValue", this.form);
      this.drawer = false;
    },
    handleDrawerForUserConfig(drawer,nodeConfig) {
      if(drawer === false){
        this.drawerForUserConfig = drawer;
        this.$emit("update:modelValue", nodeConfig);
      }
    },
    toText(nodeConfig) {
      if (nodeConfig.setType == 1) {
        if (nodeConfig.nodeUserList && nodeConfig.nodeUserList.length > 0) {
          const users = nodeConfig.nodeUserList.map((item) => item.name).join("、");
          return users;
        } else {
          return false;
        }
      }else if (nodeConfig.setType == 3) {
        if (nodeConfig.nodeRoleList && nodeConfig.nodeRoleList.length > 0) {
          const roles = nodeConfig.nodeRoleList.map((item) => item.name).join("、");
          return "角色-" + roles;
        } else {
          return false;
        }
      }else if (nodeConfig.setType == 8) {
        if (nodeConfig.nodeOrgList && nodeConfig.nodeOrgList.length > 0) {
          const orgs = nodeConfig.nodeOrgList.map((item) => item.name).join("、");
          return "组织-" + orgs;
        } else {
          return false;
        }
      }
      for (let typeListElement of this.setTypeList) {
        if(typeListElement.dictKey == nodeConfig.setType){
          return typeListElement.dictValue;
        }
      }
    },
  },
};
</script>

<style></style>
